package com.wenjiang.service.impl;

import cn.hutool.core.date.DateUtil;
import com.wenjiang.service.QuarzService;
import com.wenjiang.task.Task;
import org.bouncycastle.crypto.modes.gcm.Tables1kGCMExponentiator;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QuarzServiceImpl implements QuarzService {


    // 管理器
    @Autowired
    Scheduler scheduler;

    @Override
    public void addJob(Task task) {


        try {

            Class<?extends Job> name = ( Class<?extends Job>)Class.forName(task.getClassName());

            JobDetail jobDetail =
                    JobBuilder.newJob(name).withIdentity(task.getJobName(), task.getJobGroup()).usingJobData(task.getJobDataMap()).build();


          Trigger simpleTrigger =
                     TriggerBuilder.newTrigger().forJob(jobDetail).startAt(DateUtil.parse(task.getStartTime(),"yyyy-MM-dd HH:mm:ss")).build();

            scheduler.scheduleJob(jobDetail, simpleTrigger);


        } catch (SchedulerException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
